package main

import (
	"fmt"
	"log"
	"os"
	"time"
)

func main() {
	logger := log.New(os.Stdout, "[虚妄]", log.Lmicroseconds|log.Ldate|log.Llongfile)
	logger.Println("天下大同")
	// [虚妄]2022/11/28 20:47:03.265892 /Users/dxm/Desktop/web/go/src/go-study/go_log/main.go:12: 天下大同
}

func setLogOutput(logInfo string) {
	now := time.Now()

	openFile, err := os.OpenFile("./"+now.Format("2006-01-02-15")+".log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
	if err != nil {
		fmt.Println("open log file failed, err:", err)
		return
	}

	log.SetOutput(openFile)
	log.SetFlags(log.Llongfile | log.Lmicroseconds | log.Ldate)
	log.SetPrefix("[INFO]")
	log.Println(logInfo)
}

func goLoggerPrefix() {
	log.SetPrefix("[虚妄]")
	log.Println("阿巴阿巴阿巴阿巴")
	prefix := log.Prefix()
	fmt.Println(prefix)
	//[虚妄]2022/11/28 20:17:46 阿巴阿巴阿巴阿巴
	//[虚妄]
}

func goLoggerFlags() {
	log.SetFlags(log.Llongfile | log.Lmicroseconds | log.Ldate)
	log.Println("阿巴阿巴阿巴")
	flags := log.Flags()
	log.Println(flags)
	// 2022/11/25 13:57:06.448272 /Users/dxm/Desktop/web/go/src/go-study/go_log/main.go:14: 阿巴阿巴阿巴
}

func goLogger() {
	defer func() {
		fmt.Println("--first--")
		if err := recover(); err != nil {
			fmt.Println(err)
		}
	}()

	log.Println("server start")
	v := "很普通的"
	log.Printf("这是一条%s日志", v)
	log.Panicln("这是一条会触发panic的日志")

	// log.Fatalln
	//打印输出内容
	//退出应用程序
	//defer函数不会执行
	//和os.Exit()相比多了第一步。
	log.Fatalln("这是一条会触发fatal的日志")
}
